basicmicrofandomcom-20200214-history
MBasic: Looping Commands
Looping Commands Intro This chapter includes the "normal" BASIC commands that are included with most versions of BASIC, as well as commands specific to Atom BASIC. Read it carefully: some familiar commands may be defined somewhat differently in Atom BASIC. This chapter contains the following sections: * Looping Commands ** For...next ** Do...while ** While...wend ** Repeat...until Conventions Used in this Chapter Looping Commands Overview Looping commands repeat a number of lines (instructions) multiple times, depending on certain conditions. FOR...NEXT Repeats the instructions between FOR and NEXT a predefined number of times. Syntax :for counter = startvalue to endvalue {step stepvalue} ::statements to be executed :next ::counter is a variable used to hold the current counter value ::startvalue is the initial value of the loop counter ::endvalue is the final value of the loop counter ::stepvalue is the optional increment or decrement These values may be bit, nibble, byte, word, long or float. Startvalue, endvalue and stepvalue may be variables or constants. If STEP is omitted a stepvalue of 1 is automatically assigned. Stepvalue may be negative in which case the counter will be decremented rather than incremented. The loop will continue until the counter value falls outside the range set by endvalue. Note:' Unlike some BASICs, "next" does not have an argument in Atom BASIC, i.e. the form "next x" is not valid. ''Important: Take care not to modify the value of counter using statements within the loop. This can cause unpredictable operation, and the loop may never end.'' '''''Examples ant var byte bat var byte(11) for ant = 1 to 10 bat(ant) = ant * 20 next This simple loop will store values in the array variable "bat" as follows: :bat(0) = unchanged, bat(1) = 20, bat(2) = 40... bat(10) = 200 a var word for a = 10 to 20 step 5 {statements} next The statements will be executed 3 times with a = 10, a = 15 and a = 20. The value of "a" is incremented and tested at the end of the loop. a var word for a = 10 to 20 step 6 {statements} next The statements will be executed twice with a = 10 and a = 16. a var sword for a = 40 to 20 step -5 {statements} next The statements will be executed 5 times with a = 40, 35, 30, 25 and 20 respectively. DO... WHILE Important:' You must not nest a WHILE... WEND inside a DO... WHILE. A compiler error will result. Repeats a set of instructions as long as a given condition remains true (i.e. until the given condition becomes false). The condition is tested after the instructions have been executed. The instructions will be executed once even if the condition is initially false (see the second example below). '''''Syntax :do ::statements :while condition :::condition is any valid combination of variables, constants and logical operators. Examples a var word a = 5 do a = a * 2 hserout a while a < 100 statements The loop operates as follows: Since the test is done at the end of the loop, the final value is output even though it is greater than 100. Program execution continues with the line following "while". a var word a = 150 do a = a * 2 hserout a while a < 100 statements The loop will operate once, and output the value 300, even though the initial value is not less than 100. This is because the test is done at the end of the loop. WHILE... WEND Important:' You must not nest a WHILE... WEND inside a DO... WHILE. A compiler error will result. Repeats a set of instructions as long as a given condition remains true (i.e. until the given condition becomes false). The condition is tested before the instructions are been executed. If the condition is initially false, the instructions will never be executed. '''''Syntax :while condition ::program statements :wend :::condition is any valid combination of variables, constants and logical operators. Examples a var word a = 5 while a < 100 a = a * 2 hserout a wend program continues The loop operates as follows: On pass number 6 the test is false so the loop is not executed. Program execution continues with the line following WEND. The results are similar to the DO... WHILE loop shown above. The following example illustrates a difference between the DO... WHILE and WHILE... WEND loops. a var word a = 150 while a < 100 a = a * 2 hserout a wend program continues Unlike the DO... WHILE loop, the WHILE... WEND tests before the loop statements are executed. Since the condition is false initially, the loop is never executed and control passes to the statements following WEND. (Contrast this with the DO... WHILE loop which executes once in a similar situation.) REPEAT... UNTIL Repeats a set of instructions until a given condition becomes true (i.e. as long as the condition remains false). The condition is tested after the instructions have been executed. The instructions will be executed once even if the condition is initially true. REPEAT... UNTIL is essentially the converse of DO... WHILE. Syntax :repeat ::program statements :until condition :::condition is any valid combination of variables, constants and logical operators. Examples a var word a = 5 repeat a = a * 2 hserout a until a > 100 program continues The loop operates as follows: Program execution then continues with the line following UNTIL. If the initial value of a is greater than 100, the loop will be executed once because the test is at the end of the loop.